﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Zen4SyncDashboard.DAL
{
    public partial class activity : IDataErrorInfo
    {
        #region - IDataErrorInfo Members -

        public string Error { get { return null; } }

        public string this[string columnName]
        {
            get { return GetValidationError(columnName); }
        }

        #endregion - IDataErrorInfo Members -

        #region - Validation -

        static readonly string[] ValidatedProperties = 
        {
            "activity_preWaitingTimeInSeconds",
            "activity_postWaitingTimeInSeconds",
            "activityType",
            "activity_executionOrder",
            "activity_activityItemId"
        };

        public bool IsValid()
        {
            foreach (string property in ValidatedProperties)
                if (GetValidationError(property) != null)
                    return false;

            return true;
        }

        string GetValidationError(string propertyName)
        {
            if (!ValidatedProperties.Contains(propertyName))
                return null;

            string error = null;

            switch (propertyName)
            {
                case "activity_preWaitingTimeInSeconds":
                    error = ValidatePreWaitingTime();
                    break;
                case "activity_postWaitingTimeInSeconds":
                    error = ValidatePostWaitingTime();
                    break;
                case "activityType":
                    error = ValidateActivityType();
                    break;
                case "activity_executionOrder":
                    error = ValidateExecutionOrder();
                    break;
                case "activity_activityItemId":
                    error = ValidateActivityItemId();
                    break;
                default:
                    throw new ArgumentException("Unrecognized property: " + propertyName);
            }

            return error;
        }

        private string ValidateActivityItemId()
        {
            if (activity_activityItemId == Guid.Empty)
                return "A concrete Activity must be selected.";

            return null;    
        }

        private string ValidateExecutionOrder()
        {
            if (activity_executionOrder < 0)
                return "Execution Order must be positive.";

            return null;
        }

        private string ValidateActivityType()
        {
            if (activityType == null)
                return "An Activity Type must be selected.";

            return null;
        }

        private string ValidatePostWaitingTime()
        {
            if (activity_preWaitingTimeInSeconds < 0)
                return "Pre waiting time must be positive.";

            return null;
        }

        private string ValidatePreWaitingTime()
        {
            if (activity_postWaitingTimeInSeconds < 0)
                return "Post waiting time must be positive.";

            return null;
        }

        #endregion - Validation -
    }
}
